﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace C_homework
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void btnSelectFile_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "C# Source File (*.cs)|*.cs";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string filePath = openFileDialog1.FileName;
                string[] originalLines = RemoveEmptyLines(File.ReadAllLines(filePath));
                string[] processedLines = RemoveCommentLines(originalLines);
                //统计原始代码行数(不包括空行、空格、标点符号)
                int originalLineCount = originalLines.Length;
                //统计原始代码单词数
                int originalWordCount = CountWords(originalLines);
                //统计处理后代码行数
                int processedLineCount = processedLines.Length;
                //统计处理后代码单词数
                int processedWordCount = CountWords(processedLines);
                //更新页面文字显示
                lblOriginalLineCount.Text = $"原始代码行数：{originalLineCount}";
                lblOriginalWordCount.Text = $"原始单词数：{originalWordCount}";
                lblProcessedLineCount.Text = $"去除注释后代码行数：{processedLineCount}";
                lblProcessedWordCount.Text = $"去除注释后单词数：{processedWordCount}";

                //统计单词频率
                var wordFrequency = CountWordsFrequency(processedLines);
                //清空列表
                dataGridView1.Rows.Clear();
                //显示单词频率
                DisplayWordsFrequency(wordFrequency);
            }
        }
        
        //检查line中是否只含标点符号
        public bool IsPunctuationOnly(string line)
        {
            return Regex.IsMatch(line, @"^[^\w\s]+$");
        }

        //去除空行、空格以及标点符号
        public string[] RemoveEmptyLines(string[] lines)
        {
            var processedLines = new List<string>();
            foreach (string line in lines)
            {
                string trimmedLine = line.Trim();
                if (!string.IsNullOrEmpty(trimmedLine)&&!IsPunctuationOnly(trimmedLine))
                {
                    processedLines.Add(trimmedLine);
                }
            }   
            return processedLines.ToArray();
        }

        //去除注释
        public string[] RemoveCommentLines(string[] lines)
        {
            var processedLines = new List<string>();
            foreach (string line in lines)
            {
                string trimmedLine = line.Trim();
                if (!trimmedLine.StartsWith("//"))
                {
                    processedLines.Add(trimmedLine);
                }
            }
            return processedLines.ToArray();
        }

        //统计单词数
        public int CountWords(string[] lines)
        {
            int wordCount = 0;
            foreach(string line in lines)
            {
                string[] words = Regex.Split(line, @"[^a-zA-Z]+");
                wordCount += words.Count(n => !string.IsNullOrWhiteSpace(n) && n.Length > 1);
            }
            return wordCount;
        }

        //统计单词频率
        public Dictionary<string,int> CountWordsFrequency(string[] lines)
        {
            var wordsFrequency = new Dictionary<string,int>();
            foreach (string line in lines)
            {
                string[] words = Regex.Split(line, @"[^a-zA-Z]+");
                foreach (string word in words)
                {
                    if (!string.IsNullOrEmpty(word) && word.Length > 1)
                    {
                        if (wordsFrequency.ContainsKey(word))
                        {
                            wordsFrequency[word]++;
                        }
                        else
                        {
                            wordsFrequency[word] = 1;
                        }
                    }
                }
            }
            return wordsFrequency;
        }

        //单词及其频率可视化
        public void DisplayWordsFrequency(Dictionary<string,int> dictionary)
        {
            //排序
            var sortDictionary = dictionary.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
            //导入
            foreach(var word in sortDictionary)
            {
                dataGridView1.Rows.Add(word.Key, word.Value.ToString());
            }
        }
    }
}
